#include "menu.h"



int initialisation()
{
    /* General initialization function */
    allegro_init();
    if (install_keyboard() == -1)
    {
        allegro_message("Error! %s", allegro_error);
        return -1;
    }
    set_color_depth(32);// palette de couleur
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0)// dimension d'ecran
    {
        set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
        allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
        return -1;
    }

    if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL))
    {
        allegro_message("Sound Error: %s", allegro_error);
        return 1;
    }
    return 0;
}

int chargerSon(char chemin[])
{
    SAMPLE *samp;
    int v;
    samp=load_sample(chemin);
    if((v=allocate_voice(samp))==-1)
    {
        destroy_sample(samp);
        return 0;
    }
    return v;
}


void chargerMenu(Menu *monMenu)
{
    monMenu->images[0]=load_bitmap("images/menu/jouer.bmp",NULL);
    monMenu->images[1]=load_bitmap("images/menu/options.bmp",NULL);
    monMenu->images[2]=load_bitmap("images/menu/quitter.bmp",NULL);
    int i;
    for(i=0;i<NB_IMAGES_MENU;i++)
    {
        if (monMenu->images[i] == NULL)
        {
            allegro_message("could not load images");
            allegro_exit();
        }
    }


    monMenu->elementSelectioner=0;
}

void menu(Menu *monMenu, int *choix)
{
    int state=0;

    while(!state)
    {
        static int touchePresse;
        if(keypressed())
        {
            touchePresse=readkey();
            if ((touchePresse>>8) == KEY_DOWN)
            {
                monMenu->elementSelectioner=(monMenu->elementSelectioner+1)%3;
            }
            else if(touchePresse>>8 == KEY_UP)
            {
                if(monMenu->elementSelectioner==0) monMenu->elementSelectioner=2;
                else
                    monMenu->elementSelectioner=(monMenu->elementSelectioner-1)%3;
            }
            else if(touchePresse>>8 == KEY_ENTER)
                state=1;
        }

        draw_sprite(screen, monMenu->images[monMenu->elementSelectioner], 0, 0);

    }
    *choix=monMenu->elementSelectioner;

}


void destroy_menu(Menu *monMenu)
{
    int i;
	for (i = 0; i < NB_IMAGES_MENU; i++) {
		destroy_bitmap(monMenu->images[i]);
	}
}

